$(document).ready(function(){
    var checkCode0104 = function(){
        /*--------------------------------------------------------------
         *Define checking function */
        var checkfunc = function(ele, callback, funcCheckItem){
            var n = 0;
            var s = "";
            $(ele).find("input").each(function(index, ele){
                s += ele.value;
            });
            if ($.trim(s).length > 0) {
                $(ele).find("input").each(function(index, ele){
                    var v = parseInt(ele.value);
                    if (v > 0) {
                        if (funcCheckItem) 
                            funcCheckItem(ele, v, sum);
                        n += v;
                    }
                });
                if (!callback(n, sum)) {
                    var txt = $(ele).find(".subjectTitle").text();
                    msg += ("\"" + txt + "\" 所填人数同总人数不符!\n");
                }
            }
        };
        /* End define checking function
         --------------------------------------------------------------*/
        var me = $("div[code='0104']");
        if (me.length > 0) {
            var sum = parseInt(me.find("span.sTextOutline input").val());
            if (!(sum > 0)) 
                sum = 0;
            var msg = "";
            var divs = $("div[code]", me);
            checkfunc(divs[0], function(n, sum){
                return n >= sum
            }, function(ele, v, sum){
                if (v > sum) {
                
                    msg += "\"" + $(ele).prev("span:first").text() + "\" 不能超过 " + sum + " 人!\n";
                }
            });
            checkfunc(divs[1], function(n, sum){
                return n == sum
            });
            checkfunc(divs[2], function(n, sum){
                return n == sum
            });
            
            if (msg.length > 0) {
                alert(msg);
                return false;
            }
        }
        return true;
    }
    $.result.addValidation(checkCode0104);
});
